#include "stdafx.h"
#include "imgs.h"

#include <gtl/io/app_path.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

namespace gtl
{

namespace skins
{

imgs::imgs()
{
}

imgs::~imgs()
{
	for(size_t i = 0; i < m_imgs.size(); ++i)
	{
		m_imgs[i].second->DeleteObject();
		delete m_imgs[i].second;
	}
	m_imgs.clear();
}

unsigned int imgs::initialize(gtl::xml& node, wnd* parent_wnd)
{
	load_imgs(node);

	const gtl::xml::nodes_type& nodes = node.nodes();
	gtl::xml::const_iterator first = nodes.begin();
	gtl::xml::const_iterator last  = nodes.end();
	for(; first != last; ++first)
	{
		load_imgs(*(*first));
	}

	return pane::initialize(node, parent_wnd);
}

void imgs::load_imgs(const gtl::xml& node)
{
	const gtl::xml::attribs_type& attribs = node.attribs();
	gtl::tstr strImgPath = gtl::io::get_app_path<gtl::tstr>() + _T("skins\\default\\");

	for(size_t i = 0; i < attribs.size(); ++i)
	{
		if(attribs[i].first.icmp(_T("id")))
			continue;

		HBITMAP hBmp = (HBITMAP)LoadImage(NULL, strImgPath + attribs[i].second, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
		if(hBmp == NULL)
			continue;

		CBitmap* pBitmap = new CBitmap;
		pBitmap->Attach(hBmp);
		m_imgs.push_back(std::make_pair(attribs[i].first, pBitmap));
	}
}

bitmap* imgs::get_image(const gtl::tstr& path)
{
	for(size_t i = 0; i < m_imgs.size(); ++i)
	{
		if(!path.icmp(m_id + _T(".") + m_imgs[i].first))
			continue;

		return m_imgs[i].second;
	}

	return NULL;
}

} // end of namespace skins

} // end of namespace gtl